#include "suspendscrollbarlistwidget.h"

SuspendScrollBarListWidget::SuspendScrollBarListWidget(QWidget *parent) : QListWidget (parent)
{
    setVerticalScrollBarPolicy(Qt::ScrollBarPolicy::ScrollBarAlwaysOff);
    setHorizontalScrollBarPolicy(Qt::ScrollBarPolicy::ScrollBarAlwaysOff);
    m_scrollBar = new SuspendScrollBar(this);

    connect(m_scrollBar, SIGNAL(valueChanged(int)), this, SLOT(slotValueChanged(int)));
    connect(verticalScrollBar(), SIGNAL(valueChanged(int)), m_scrollBar, SLOT(slotValueChanged(int)));
    connect(verticalScrollBar(), SIGNAL(rangeChanged(int, int)), m_scrollBar, SLOT(slotRangeChanged(int, int)));
}

SuspendScrollBarListWidget::~SuspendScrollBarListWidget()
{
    delete m_scrollBar;
    m_scrollBar = nullptr;
}

void SuspendScrollBarListWidget::slotValueChanged(int value)
{
    this->verticalScrollBar()->setValue(value);
}

void SuspendScrollBarListWidget::resizeEvent(QResizeEvent *e)
{
    int ax = this->width() - 9;
    m_scrollBar->setGeometry(ax, 1, 8, height() - 2);
    QListWidget::resizeEvent(e);
}

void SuspendScrollBarListWidget::enterEvent(QEvent *e)
{
    if (m_scrollBar->maximum() > 0) {
        m_scrollBar->show();
    }
    QListWidget::enterEvent(e);
}

void SuspendScrollBarListWidget::leaveEvent(QEvent *e)
{
    m_scrollBar->hide();
    QListWidget::leaveEvent(e);
}
